package com.rede.didiok.admin.restapi.admin;

import java.util.Map;

import javax.annotation.Resource;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import com.rede.didiok.admin.annotion.AuthorityVerify.AuthorityVerify;
import com.rede.didiok.admin.annotion.OperationLogger.OperationLogger;
import com.rede.didiok.admin.service.SysDictDataService;
import com.rede.didiok.admin.support.RankMemberSupport;
import com.rede.didiok.common.constant.conf.MessageConf;
import com.rede.didiok.common.constant.conf.SysConf;
import com.rede.didiok.common.enums.ResourceTypeEnum;
import com.rede.didiok.common.feign.CommunityFeignClient;
import com.rede.didiok.common.feign.RankFeignClient;
import com.rede.didiok.common.feign.SearchFeignClient;
import com.rede.didiok.common.utils.JsonUtils;
import com.rede.didiok.common.utils.ResultUtil;

/**
 * 索引维护 ReastApi
 *
 * @author Rede
 * @date 2023年11月15日16:44:27
 */
@RestController
@RequestMapping("/admin/search")
@Api(value = "索引维护相关接口", tags = {"索引维护相关接口"})
@Slf4j
public class SearchIndexRestApi {

    @Resource
    private RankFeignClient rankFeignClient;
    @Resource
    private RankMemberSupport rankMemberSupport;
    @Resource
    private CommunityFeignClient communityFeignClient;
    @Resource
    private SearchFeignClient searchFeignClient;
    @Resource
    private SysDictDataService sysDictDataService;


    @AuthorityVerify
    @OperationLogger(value = "清空Es索引")
    @ApiOperation(value = "清空Es索引", notes = "清空Es索引")
    @PostMapping("/cleanIndex")
    public String cleanIndex() {
        String result = searchFeignClient.cleanEsIndex();
        Map<String, Object> blogMap = (Map<String, Object>) JsonUtils.jsonToObject(result, Map.class);
        if (SysConf.SUCCESS.equals(blogMap.get(SysConf.CODE))) {
            return ResultUtil.successWithMessage(MessageConf.OPERATION_SUCCESS);
        } else {
            return ResultUtil.errorWithMessage(blogMap.get(SysConf.MESSAGE).toString());
        }
    }

    @AuthorityVerify
    @OperationLogger(value = "批量同步榜单成员索引")
    @ApiOperation(value = "批量同步榜单成员索引", notes = "批量同步榜单成员索引")
    @GetMapping("/batchInitElasticIndex")
    public String initRankMemberElasticIndex(@RequestParam("resourceType") ResourceTypeEnum resourceType) {
        return rankMemberSupport.initRankMemberElasticIndex(resourceType);
    }

}
